package com.jy.modules.cims.component.jieyue;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.jy.modules.cims.common.CimsConstant;
import com.jy.modules.cims.common.sysUtils.CollectionsUtils;
import com.jy.modules.cims.common.sysUtils.SearchParamUtil;
import com.jy.modules.cims.component.api.CReceiveCreditImpl;
import com.jy.modules.cims.component.api.IJieYueSend;
import com.jy.modules.cims.component.common.service.TCMmCreditDetailService;
import com.jy.modules.cims.data.common.dto.TCMmCreditDetailDTO;
import com.jy.modules.cims.data.dao.DealDao;
import com.jy.modules.cims.data.dto.DealDto;
import com.jy.modules.cims.data.interact.api.JieYueResponseData;
import com.jy.modules.cims.data.interact.api.request.RequestTransferResultSearchDTO;
import com.jy.modules.cims.data.interact.api.request.TransferCreidtReceiveDTO;
import com.jy.modules.cims.data.interact.api.response.ResponseTransferResultSearchDTO;

@Service("com.jy.modules.cims.component.jieyue.CSynProcessTransferResultImpl")
public class CSynProcessTransferResultImpl implements ICSynProcess {

	private static final Logger logger = LoggerFactory.getLogger(CReceiveCreditImpl.class);
	
	@Autowired
	@Qualifier("jieYueSendService")
	private IJieYueSend jieYueSend;
	
	@Autowired
	public DealDao dealDao;

	@Autowired
	@Qualifier("com.jy.modules.cims.component.common.service.TCMmCreditDetailService")
	private TCMmCreditDetailService creditDetailService;
	
	/**
	 * @Description 根据机构编码，查询系统需要同步更新债权审批结果。 调用外围系统接口债权审批结果。更新系统债权明细的债权状态。
	 * @param cooperorgCode
	 *            机构编码
	 * @throws Exception
	 * @author 
	 * @date 2016-10-10
	 */
	@Override
	public void doSomething(String cooperorgCode) throws Exception {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		DealDto dto = new DealDto();
		dto.setCooperorgCode(cooperorgCode);
		paramMap.put("dto", dto);
		List<TransferCreidtReceiveDTO> creditList = dealDao.getCreditAuditByInterface(paramMap);
		if (CollectionsUtils.isEmpty(creditList)) {
			return;
		}

		RequestTransferResultSearchDTO content = new RequestTransferResultSearchDTO();
		content.setCreditList(creditList);
		JieYueResponseData responseData = jieYueSend.searchTransferResult(cooperorgCode, content);
		if (responseData.getData() == null) {
			logger.info(cooperorgCode + "查询审批结果失败!");
			return;
		}

		ResponseTransferResultSearchDTO rs = (ResponseTransferResultSearchDTO) responseData.getData();
		if (!CimsConstant.JIEYU_RET_CODE_SUC.equals(rs.getResult())) {
			return;
		}

		List<TransferCreidtReceiveDTO> resultcreditList = rs.getCreditList();
		if (CollectionsUtils.isEmpty(resultcreditList)) {
			return;
		}

		TCMmCreditDetailDTO creditDetailDTO = null;
		for (TransferCreidtReceiveDTO requestTransferCreidtDTO : resultcreditList) {
			String creditEachStatus = requestTransferCreidtDTO.getCreditEachStatus();
			if (CimsConstant.CREDIT_STATUS_WIN.equals(creditEachStatus)) {
				logger.info("债权转让编号：【{}】审批通过！", requestTransferCreidtDTO.getTransferId());

				dealDao.updateCreditDealDetailByInterface(SearchParamUtil.getSearchParam(requestTransferCreidtDTO));
			} else if (CimsConstant.CREDIT_STATUS_LOSE.equals(creditEachStatus)) {
				logger.info("债权转让编号：【{}】审批拒绝！", requestTransferCreidtDTO.getTransferId());

				creditDetailDTO = new TCMmCreditDetailDTO();
				creditDetailDTO.setCreditCode(requestTransferCreidtDTO.getCreditCode());
				creditDetailDTO.setTransferId(requestTransferCreidtDTO.getTransferId());
				creditDetailDTO.setCreditSource(CimsConstant.CREDIT_SOURCE_CRE);
				List<TCMmCreditDetailDTO> creditDetailList = creditDetailService.searchTCMmCreditDetail(SearchParamUtil
						.getSearchParam(creditDetailDTO));
				if (CollectionsUtils.isEmpty(creditDetailList)) {
					continue;
				}
				creditDetailService.batchUpdateTCMmCreditStatus(creditDetailList.get(0).getId().toString(), CimsConstant.CREDIT_STATUS_LOSE);
			} else if (CimsConstant.CREDIT_STATUS_WAY.equals(creditEachStatus)) {
				logger.info("债权转让编号：【{}】正在审批中！", requestTransferCreidtDTO.getTransferId());
			}
		}
	}
}
